package com.foreknow.controller;

import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;
//import jakarta.servlet.ServletException;
//import jakarta.servlet.http.HttpServlet;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


public class AddMessageBookServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        PrintWriter writer = response.getWriter();

        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        String title = request.getParameter("title");
        String content = request.getParameter("content");


        //创建时间
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH：mm:ss");
        String time = sdf.format(date);


        Guestbook guestbook=new Guestbook();
        guestbook.setName(name);
        guestbook.setPhone(phone);
        guestbook.setEmail(email);
        guestbook.setTitle(title);
        guestbook.setContent(content);
        guestbook.setCreatedTime(time);
        GuestbookService guestbookService=new GuestbookServiceImpl();
        boolean add = guestbookService.add(guestbook);
        if (add) {
            //直接转发资源（请求转发）
           //request.getRequestDispatcher("/user/get").forward(request,response);
           response.sendRedirect(request.getContextPath()+"/user/manager?q=list");

        }else {
            writer.println("添加失败");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request,response);
    }
}
